﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace MountainORocks
{
    class PowerController
    {
        private int maxPower;
        private int currentPower;

        private int selectedPower;

        private int WIDTH = 300;
        private int HEIGHT = 20;

        private int THICKNESS = 3;

        private Vector2 topCorner;

        private Texture2D pixel; // used for drawing lines

        public PowerController(GraphicsDevice graphics)
        {
            this.maxPower = 40000;
            this.currentPower = this.maxPower;

            // creating an empty pixel
            this.pixel = new Texture2D(graphics, 1, 1);
            pixel.SetData(new[] { Color.White });

            // set top corner
            this.topCorner = new Vector2(40, 60);

            // set selected power 
            this.selectedPower = 0;
        }

        public void SetMaxPower(int maxPower)
        {
            this.maxPower = maxPower;
        }

        public void Reset()
        {
            this.currentPower = this.maxPower;
        }

        public void AddPower(int power)
        {
            this.currentPower += power;
            if (this.currentPower > this.maxPower) this.currentPower = this.maxPower;
        }

        public int getPower()
        {
            return currentPower;
        }

        public bool RemovePower(int power)
        {
            if (currentPower - power < 0)
                return false;
            this.currentPower -= power;
            return true;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            // border
            DrawRectangle(spriteBatch, new Rectangle((int)topCorner.X, (int)topCorner.Y, WIDTH, HEIGHT), Color.White, THICKNESS);

            if (currentPower > 0)
            {

                // ratio
                float ratio = (float)currentPower / (float)maxPower;

                float newWidht = WIDTH * ratio;

                if (selectedPower == 0 || selectedPower > currentPower)
                {
                    // current power
                    DrawRectangle(spriteBatch, new Rectangle((int)topCorner.X + THICKNESS, (int)topCorner.Y + THICKNESS, (int)newWidht - 2 * THICKNESS, HEIGHT - 2 * THICKNESS),
                        Color.Green, HEIGHT / 2);
                }
                else
                {
                    ratio = (float)selectedPower / (float)maxPower;
                    float selectedWidth = WIDTH * ratio;
                    newWidht -= selectedWidth;
                    // current power
                    DrawRectangle(spriteBatch, new Rectangle((int)topCorner.X + THICKNESS, (int)topCorner.Y + THICKNESS, (int)newWidht - 2 * THICKNESS, HEIGHT - 2 * THICKNESS),
                        Color.Green, HEIGHT / 2);
                    // current power
                    DrawRectangle(spriteBatch, new Rectangle((int)topCorner.X + (int)newWidht - THICKNESS, (int)topCorner.Y + THICKNESS, (int)selectedWidth, HEIGHT - 2 * THICKNESS),
                        Color.Red, HEIGHT / 2);

                }
            }


        }

        public void setSelected(int selected)
        {
            this.selectedPower = selected;
        }

        
        private void DrawLineSegment(SpriteBatch spriteBatch, Vector2 point1, Vector2 point2, Color color, int lineWidth)
        {
            float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
            float length = Vector2.Distance(point1, point2);
            spriteBatch.Draw(this.pixel, point1, null, color,
            angle, Vector2.Zero, new Vector2(length, lineWidth),
            SpriteEffects.None, 0f);
        }

        private void DrawPolygon(SpriteBatch spriteBatch, Vector2[] vertex, int count, Color color, int lineWidth)
        {
            if (count > 0)
            {
                for (int i = 0; i < count - 1; i++)
                {
                    DrawLineSegment(spriteBatch, vertex[i], vertex[i + 1], color, lineWidth);
                }
                DrawLineSegment(spriteBatch, vertex[count - 1], vertex[0], color, lineWidth);
            }
        }

        private void DrawRectangle(SpriteBatch spriteBatch, Rectangle rectangle, Color color, int lineWidth)
        {
            Vector2[] vertex = new Vector2[4];
            vertex[0] = new Vector2(rectangle.Left, rectangle.Top);
            vertex[1] = new Vector2(rectangle.Right, rectangle.Top);
            vertex[2] = new Vector2(rectangle.Right, rectangle.Bottom);
            vertex[3] = new Vector2(rectangle.Left, rectangle.Bottom);

            DrawPolygon(spriteBatch, vertex, 4, color, lineWidth);
        }
    }
}
